package com.hjj.controller;

import com.hjj.beans.User;
import com.hjj.dto.ResultDTO;
import com.hjj.exception.LoginException;
import com.hjj.services.UserServices;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    @Qualifier("userServicesImp")
    UserServices userServices;

    @Autowired
    @Qualifier("userServicesFilterImp")
    UserServices userServices1;

    @RequestMapping(value = "in.action", method = RequestMethod.POST)
    public ResultDTO in(HttpServletResponse response, HttpSession httpSession, String loginAct, String loginPwd, String rememberMe) throws LoginException {

        User user = userServices.login(loginAct, loginPwd);

        if (user == null) throw new LoginException("用户名或密码错误");

        // 登录成功将数据传入HttpSession中
        httpSession.setAttribute("user", user);

        // cookie创建三部曲
        // 1——设置cookie的键值对(键和值必须只能是String类型)
        // 2——设置cookie的键值对作用域，也就是那个地址可以用(这里设置跟地址，意味着任何地址都可以调用cookie里面的值)
        // 3——设置cookie的年龄

        Cookie cookieLoginAct = new Cookie("loginAct", loginAct);
        Cookie cookieLoginPwd = new Cookie("loginPwd", DigestUtils.md5Hex(loginPwd));

        // 设置cookie的键值对作用域，也就是那个地址可以用(这里设置跟地址，意味着任何地址都可以调用cookie里面的值)
        cookieLoginAct.setPath("/");
        cookieLoginPwd.setPath("/");


        // System.out.println("checked: " + rememberMe);
        // 判断是否需要记住用户(10天)
        if ("true".equals(rememberMe)) {
            // 记住
            // 设置cookie的年龄(以秒为单位)
            cookieLoginAct.setMaxAge(60 * 60 * 24 * 10);
            cookieLoginPwd.setMaxAge(60 * 60 * 24 * 10);
        } else {
            // 删除记住的用户
            // 设置cookie的年龄
            cookieLoginAct.setMaxAge(0);
            cookieLoginPwd.setMaxAge(0);
        }

        // 响应的时候告诉浏览器添加我们的cookie
        response.addCookie(cookieLoginAct);
        response.addCookie(cookieLoginPwd);


        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        resultDTO.setData(user);

        return resultDTO;
    }

    @RequestMapping("userInfo.action")
    public ResultDTO userInfo(HttpSession httpSession) throws LoginException {
        // 获取HttpSession中的登录者信息
        User user = (User) httpSession.getAttribute("user");

        // 判断是否有登录者信息
        if (user == null) {
            throw new LoginException("还没有登录");
        }

        // DTO数据封装
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        resultDTO.setData(user);

        return resultDTO;
    }

    @RequestMapping("out.action")
    public ResultDTO out(HttpServletResponse response, HttpSession httpSession) throws LoginException {
        // 删除HttpSession里面的数据
        httpSession.removeAttribute("user");

        // 删除cookies中的数据(这里只能用设置年龄的时间来删除数据)
        Cookie cookieLoginAct = new Cookie("loginAct", "");
        Cookie cookieLoginPwd = new Cookie("loginPwd", "");

        // 设置cookie的键值对作用域，也就是那个地址可以用
        cookieLoginAct.setPath("/");
        cookieLoginPwd.setPath("/");

        // 设置cookie的年龄(以秒为单位)
        cookieLoginAct.setMaxAge(0);
        cookieLoginPwd.setMaxAge(0);

        // 响应的时候告诉浏览器添加我们的cookie
        response.addCookie(cookieLoginAct);
        response.addCookie(cookieLoginPwd);

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);

        return resultDTO;
    }

    @RequestMapping("automaticWriting.action")
    public ResultDTO automaticGetUser(HttpServletRequest request) throws LoginException {

        Cookie[] cookies = request.getCookies();

        if (cookies == null) throw new LoginException("没有自动登录的用户数据");

        HashMap<String, String> hashMap = new HashMap<>();

        for (Cookie cookie : cookies) {
            if ("loginAct".equals(cookie.getName())){
                hashMap.put("loginAct", cookie.getValue());
            }else if ("loginPwd".equals(cookie.getName())){
                hashMap.put("loginPwd", cookie.getValue());
            }
        }

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        resultDTO.setData(hashMap);

        return resultDTO;
    }

    @RequestMapping("automaticLogin.action")
    public ResultDTO automaticLogin(String loginAct, String loginPwd, String rememberMe) throws LoginException {
        User login = userServices1.login(loginAct, loginPwd);

        if (login == null) throw new LoginException("没有该用户");

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);

        return resultDTO;
    }
}
